package com.dodoke.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dodoke.dto.RetailWarehouseDTO;
import com.dodoke.entity.RetailWarehouse;
import com.dodoke.mapper.RetailWarehouseMapper;
import com.dodoke.service.IRetailWarehouseService;
import com.dodoke.vo.RetailWarehouseAllListVO;
import com.dodoke.vo.RetailWarehouseListVO;
import org.springframework.cglib.core.Converter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓库信息业务层实现
 */
@Service
public class RetailWarehouseServiceImpl extends ServiceImpl<RetailWarehouseMapper, RetailWarehouse> implements IRetailWarehouseService {
    @Resource
    private RetailWarehouseMapper retailWarehouseMapper;


    /**
     * 仓库信息分页查询
     *
     * @param currentPage        当前页码
     * @param pageSize           每页显示条数
     * @param retailWarehouseDTO 模糊查询接收参数
     * @return
     */
    @Override
    public Page<RetailWarehouseListVO> getListByPage(Integer currentPage, Integer pageSize, RetailWarehouseDTO retailWarehouseDTO) {
        Page<RetailWarehouse> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<RetailWarehouse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(RetailWarehouse::getId, RetailWarehouse::getName, RetailWarehouse::getNo, RetailWarehouse::getAddress)
                .orderByAsc(RetailWarehouse::getId)
                .like(!StringUtils.isEmpty(retailWarehouseDTO.getAddress()), RetailWarehouse::getAddress, retailWarehouseDTO.getAddress())
                .and(!StringUtils.isEmpty(retailWarehouseDTO.getNameAndCode()), res -> {
                    res.like(RetailWarehouse::getName, retailWarehouseDTO.getNameAndCode());
                    res.or();
                    res.like(RetailWarehouse::getNo, retailWarehouseDTO.getNameAndCode());
                });
        page = retailWarehouseMapper.selectPage(page, queryWrapper);
        if (page.getPages()<currentPage){
            currentPage -= 1;
            page = retailWarehouseMapper.selectPage(page, queryWrapper);
        }
        List<RetailWarehouseListVO> recordsVO = page.getRecords().stream()
                .map(e -> JSON.parseObject(JSON.toJSONString(e), RetailWarehouseListVO.class))
                .collect(Collectors.toList());
        Page<RetailWarehouseListVO> resPage = new Page<>(currentPage, pageSize);
        resPage.setRecords(recordsVO);
        resPage.setTotal(page.getTotal());
        resPage.setPages(page.getPages());
        return resPage;
    }

    /**
     * 仓库信息新增
     *
     * @param retailWarehouse
     * @return
     */
    @Override
    public int saveRetailWarehouse(RetailWarehouse retailWarehouse) {
        retailWarehouse.setDelFlag("0");
        return retailWarehouseMapper.insertRetailWarehouse(retailWarehouse);
    }

    /**
     * 仓库信息更新
     *
     * @param retailWarehouse
     * @return
     */
    @Override
    public int editRetailWarehouse(RetailWarehouse retailWarehouse) {
        return retailWarehouseMapper.updateRetailWarehouse(retailWarehouse);
    }

    /**
     * 根据id获取仓库信息
     *
     * @param id
     * @return
     */
    @Override
    public RetailWarehouseListVO getById(Long id) {
        RetailWarehouse warehouse = retailWarehouseMapper.selectById(id);
        RetailWarehouseListVO retailWarehouseListVO = JSON.parseObject(JSON.toJSONString(warehouse), RetailWarehouseListVO.class);
        return retailWarehouseListVO;
    }

    /**
     * 获取所有仓库列表
     *
     * @return
     */
    @Override
    public List<RetailWarehouseAllListVO> getAllList() {
        return retailWarehouseMapper.selectAllList();
    }
}
